using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BaseForum.Model;
using WebForum.App_Data;

namespace WebForum
{
    public partial class ViewTopic : System.Web.UI.Page
    {
        private static string action;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                action = "new";
                if (Request.QueryString["id"] != null)
                {
                    Topic topic = TopicDataSource.Get(Int32.Parse(Request.QueryString["id"]));
                    LabelTopicTitle.Text = "Topic: " + topic.Title;
                    LabelTopicContent.Text = topic.Content;
                    HiddenFieldTopicId.Value = topic.Id.ToString();
                }
            }
        }

        protected void ButtonReply_Click(object sender, EventArgs e)
        {
            if (Session["user"] == null) { Common.Show("You must login!", "Home.aspx"); return; }
            action = "new";
            Panel2.Visible = true;
            Panel1.Visible = false;
        }

        protected void ButtonSubmit_Click(object sender, EventArgs e)
        {
            if (action == "new")
            {
                Topic pTopic = TopicDataSource.GetFull(Int32.Parse(Request.QueryString["id"].ToString()));
                Topic sTopic = new Topic();
                sTopic.Forum = pTopic.Forum;
                sTopic.User = (User)Session["user"];
                sTopic.ParrentTopic = pTopic;
                sTopic.Title = TextBoxReplyTitle.Text;
                sTopic.Content = TextBoxReplyContent.Text;
                sTopic.PostDate = new DateTime();
                if (TopicDataSource.Insert(sTopic))
                    Common.Show("Reply Posted!", "ViewTopic.aspx?id=" + pTopic.Id);
                else
                    Common.Show("Reply faild!", Request.UrlReferrer.ToString());
            }
            else if (action == "edit")
            {
                Topic topic = TopicDataSource.GetFull(Common.ParseInt32(HiddenFieldTopicEditID.Value.ToString()));
                topic.Title = TextBoxReplyTitle.Text;
                topic.User = (User)Session["user"];
                topic.Content = TextBoxReplyContent.Text;
                string urlRedirect = topic.ParrentTopic != null
                                         ? "ViewTopic.aspx?id=" + topic.ParrentTopic.Id
                                         : "ViewTopic.aspx?id=" + topic.Id;
                if (TopicDataSource.Update(topic))
                    Common.Show("Update Posted!", urlRedirect);
                else
                    Common.Show("Update Faild!", Request.UrlReferrer.ToString());
            }
        }

        protected void ButtonEdit2_Click(object sender, EventArgs e)
        {
            if (Session["user"] == null) { Common.Show("You must login!", "Home.aspx"); return; }
            Button button = sender as Button;
            GridViewRow row = (GridViewRow)button.NamingContainer;
            int fId = Common.ParseInt32((row.FindControl("HiddenFieldSubTopicId") as HiddenField).Value);
            if (TopicDataSource.IsOwner((User)Session["user"], TopicDataSource.Get(fId)))
            {
                Panel2.Visible = true;
                Panel1.Visible = false;
                Topic topic = TopicDataSource.Get(fId);
                TextBoxReplyTitle.Text = topic.Title;
                TextBoxReplyContent.Text = topic.Content;
                action = "edit";
                HiddenFieldTopicEditID.Value = topic.Id.ToString();
                //Common.Show(fId.ToString(), Request.UrlReferrer.ToString());
            }
            else
            {
                Common.Show("You cant edit! Because you arent owner", Request.UrlReferrer.ToString());
            }
        }

        protected void ButtonEdit1_Click(object sender, EventArgs e)
        {
            if (Session["user"] == null) { Common.Show("You must login!", "Home.aspx"); return; }
            if (TopicDataSource.IsOwner((User)Session["user"], TopicDataSource.Get(Int32.Parse(Request.QueryString["id"]))))
            {
                int fId = Common.ParseInt32(HiddenFieldTopicId.Value);
                Panel2.Visible = true;
                Panel1.Visible = false;
                Topic topic = TopicDataSource.Get(fId);
                TextBoxReplyTitle.Text = topic.Title;
                TextBoxReplyContent.Text = topic.Content;
                action = "edit";
                HiddenFieldTopicEditID.Value = topic.Id.ToString();
                //Common.Show(fId.ToString(), Request.UrlReferrer.ToString());
            }
            else
            {
                Common.Show("You cant edit! Because you arent owner", Request.UrlReferrer.ToString());
            }
        }

        protected void ButtonCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(Request.RawUrl);
        }
    }
}
